package com.bankcomm.ui.specialwidget;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsoluteLayout;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bankcomm.R;
import com.bankcomm.bean.AirTicket;
import com.bankcomm.bean.Ticket;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.BaseViewCopy;
import com.bankcomm.ui.specialwidget.LPAirList.MyAirList.MyAdapter;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.Utils;

/**
 * 机票列表 控件 
 * @author sun.tao
 *
 * @date 2013-6-6 上午09:41:56
 */
public class LPAirList extends Component {
	List<Ticket> ticket_list;
	MyAdapter adapter;
	private static final int KEY_PRICE = 2;
	private static final int KEY_DATE = 1;

	@Override
	public void initRealView(Activity activity, String nodeValue) {
		realView_ = new MyAirList(activity);
	}
	
	public class MyAirList extends ListView implements CompositedComponent {
		private Context context;
		/** 票据类型 */
		Ticket mTicket;
		String url;
		
		public MyAirList(final Context context) {
			super(context);
			
			this.context = context;
			mTicket = new Ticket();
			
			setFadingEdgeLength(0);
			setCacheColorHint(Color.TRANSPARENT);
			setBackgroundColor(Color.TRANSPARENT);
			setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			
			setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			
			// 获得 data 属性的值, 取得所有的票
			Object objData = getPropertyByName("data");
			if (objData != null) {
				ticket_list = mTicket.parse((String)objData, Ticket.AIR_NUM);
			}
			
			adapter = loadingAdapter(context, ticket_list);
			setAdapter(adapter);
			
			/**
			 * OnItemClick点击事件
			 * */
			setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int position, long arg3) {
					String url = ((AirTicket)ticket_list.get(position)).getUrl();
					LPMid.getInstance().um_.dealWithLink(url, (BaseViewCopy)context);
				}
			});
			
			// 设置选中项的背景色。
			this.setSelector(R.drawable.list_item_bg);
		}
		
		/**
		 * 加载票类控件适配器
		 * 
		 * @param context
		 * @param ticketType
		 */
		private MyAdapter loadingAdapter(Context context, List<Ticket> ticket_list) {
			return new MyAdapter(context, ticket_list);
		}

		@Override
		public Component composited() {
			return LPAirList.this;
		}
		
		class MyAdapter extends BaseAdapter {
			Context c;
			
			List<Ticket> list;
			
			public MyAdapter(Context context,List<Ticket> ticket_list) {
				c = context;
				this.list = ticket_list;
			}
			
			@Override
			public int getCount() {
				return list.size();
			}

			@Override
			public Object getItem(int position) {
				return list.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}
			
			public void setList(List<Ticket> list) {
				this.list = list;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ViewHolder holder = null;
				if (null == convertView) {
					convertView = LayoutInflater.from(c).inflate(R.layout.air_list_item, null);

					holder = new ViewHolder();
					
					holder.startAirport = (TextView) convertView.findViewById(R.id.startAirport);
					holder.endAirport = (TextView) convertView.findViewById(R.id.endAirport);
					holder.iconvoyage = (ImageView) convertView.findViewById(R.id.iconvoyage);
					holder.voyageName = (TextView) convertView.findViewById(R.id.voyageName);
					holder.numAndSeat = (TextView) convertView.findViewById(R.id.numAndSeat);
					holder.org = (TextView) convertView.findViewById(R.id.org);
					holder.dst = (TextView) convertView.findViewById(R.id.dst);
					holder.amount = (TextView) convertView.findViewById(R.id.amount);
					holder.shipType = (TextView) convertView.findViewById(R.id.shipType);
					holder.shipPrice = (TextView) convertView.findViewById(R.id.shipPrice);

					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				
				String imgName = ((AirTicket)list.get(position)).getIconvoyage();
				// 得到图片资源ID imgName只是名称不应该有后缀
//				int imgId = c.getResources().getIdentifier(imgName, "drawable", c.getPackageName());
//				Utils.printOutToConsole("imgId ----- >>>>" + imgId);
				// 得到 Drawable
//				Drawable drawable = getResources().getDrawable(imgId);
				
				Bitmap bitmap = Utils.getBitmapFromResources(context, imgName);
				
				holder.startAirport.setText(((AirTicket)list.get(position)).getStartAirport());
				holder.endAirport.setText(((AirTicket)list.get(position)).getEndAirport());
				holder.iconvoyage.setImageBitmap(bitmap);
				holder.voyageName.setText(((AirTicket)list.get(position)).getVoyageName());
				holder.numAndSeat.setText(((AirTicket)list.get(position)).getNumAndSeat());
				holder.org.setText(((AirTicket)list.get(position)).getOrg());
				holder.dst.setText(((AirTicket)list.get(position)).getDst());
				holder.amount.setText(((AirTicket)list.get(position)).getAmount());
				holder.shipType.setText(((AirTicket)list.get(position)).getShipType());
				holder.shipPrice.setText(((AirTicket)list.get(position)).getShipPrice());
				
				return convertView;
			}
			
			class ViewHolder {
				TextView startAirport;
				TextView endAirport;
				ImageView iconvoyage;
				TextView voyageName;
				TextView numAndSeat;
				TextView org;
				TextView dst;
				TextView amount;
				TextView shipType;
				TextView shipPrice;
			}
		}

	}
	
	public void onClickAction(int id) {
		switch (id) {
		case KEY_PRICE:
			Collections.sort(ticket_list, new SortPrice());
			adapter.notifyDataSetChanged();
			((MyAirList)realView_).setAdapter(adapter);
			((MyAirList)realView_).invalidate();
			this.invalidate();
			break;
		case KEY_DATE:
			Collections.sort(ticket_list, new SortDate());
			MyAdapter adapter2=(MyAdapter) ((MyAirList)realView_).getAdapter();
			adapter2.notifyDataSetChanged();
			((MyAirList)realView_).setAdapter(adapter2);
			((MyAirList)realView_).invalidate();
			this.invalidate();
			break;
		}
	}
	
	/** 时间排序 */
	class SortDate implements Comparator<Ticket> {
		
		@Override
		public int compare(Ticket object1, Ticket object2) {
			AirTicket ticket1 = (AirTicket) object1;
			AirTicket ticket2 = (AirTicket) object2;
			int t1 = timeLong(ticket1.getStartAirport());
			int t2 = timeLong(ticket2.getStartAirport());
			
			return Double.compare(t1, t2);
		}
		
		/** 起始时长 */
		int timeLong(String startAirport){
			int sMin = 0;
			int i = startAirport.indexOf(":");
			String sH = startAirport.substring(0, i);
			String sM = startAirport.substring(i + 1, startAirport.length());
			
			sMin = Integer.parseInt(sH) * 60 + Integer.parseInt(sM);
			
			return sMin;
		}
		
	}
	
	
	/** 价格排序 */
	class SortPrice implements Comparator<Ticket> {
		
		@Override
		public int compare(Ticket object1, Ticket object2) {
			AirTicket ticket1 = (AirTicket) object1;
			AirTicket ticket2 = (AirTicket) object2;
			double amount1 = amount(ticket1.getAmount());
			double amount2 = amount(ticket2.getAmount());
			return Double.compare(amount1, amount2);
		}
		
		/** 类型转换 */
		double amount(String amountPrice) {
			double i = Double.parseDouble(amountPrice);
			if (amountPrice.startsWith("￥")) {
				i = Double.parseDouble(amountPrice.substring(1, amountPrice.length()));
			}
			return i;
		}
	}
	
	@Override
	public void mouldH() throws Exception {
		super.mouldH();
		
		int totalHeight = 0;
		int childSize = (adapter != null) ? adapter.getCount() : 0;
		if(childSize > 0){
			for (int i = 0; i < childSize; i++) {
				View listItem = adapter.getView(i, null, (MyAirList) this.realView_);
				listItem.measure(0, 0);
				totalHeight += listItem.getMeasuredHeight();
			}
			totalHeight += ((MyAirList) this.realView_).getDividerHeight() * (childSize - 1);
			AbsoluteLayout.LayoutParams lp = (android.widget.AbsoluteLayout.LayoutParams) getLayoutParams();
			lp.height = totalHeight;
			setLayoutParams(lp);
		}
	}
	@Override
	public void releaseResource(Component component) {
		super.releaseResource(component);
		if(ticket_list != null){
			ticket_list.clear();
			ticket_list = null;
		}
	}
}
